package data

import (
	"fmt"
	"mychat/internal/common/logger"
	"mychat/internal/conf"
	"mychat/internal/data/dao"
	"mychat/internal/data/mongo"
	"mychat/internal/data/redis"
)

type Data struct {
	dc        *conf.Data
	lg        logger.Logger
	UserDao   dao.UserDao
	FriendDao dao.FriendDao
	Redis     redis.Redis
	Mongo     mongo.Mongo
}

func NewData(dc *conf.Data, lg logger.Logger) *Data {
	db, err := dao.CreateAndInitMysqlDb(dc.Mysql)
	if err != nil {
		panic(fmt.Errorf("create and init mysqldb failed||err=%v", err))
	}

	db = db.Debug()

	userDao := dao.NewUserDao(db, lg)
	friendDao := dao.NewFriendDao(db, lg)
	redisdb := redis.NewRedis(dc.Redis, lg)
	mgdb := mongo.NewMongo(dc.Mongo, lg)

	return &Data{
		dc:        dc,
		lg:        lg,
		UserDao:   userDao,
		FriendDao: friendDao,
		Redis:     redisdb,
		Mongo:     mgdb,
	}
}
